//
//  Gesture.swift
//  NaiCha
//
//  Created by 唐安坤 on 2024/9/16.
//

import SwiftUI

struct Gesture: View {
    
    @GestureState var longGesture = false
    @GestureState var dragState = CGSize.zero
    @GestureState var rotate = Angle(degrees: 0)
    @GestureState var magnify:CGFloat = 1
    @State var startPosition = CGSize.zero
    @State var scale:Bool = true
    @State var rotetion:Bool = true
    @State var scaleF:CGFloat = 1
    var body: some View {
        VStack{
            RoundedRectangle(cornerRadius: 20).foregroundColor(.orange).frame(width: 300, height: 450, alignment: .leading).overlay {
                //双击
                VStack(spacing:20){
                    Image(systemName: "basketball.fill")
                        .font(.system(size: 90))
                        .foregroundStyle(.pink)
                        .scaleEffect(CGSize(width: scale ? 1 :0.5, height: scale ? 1 :0.5))
                        .gesture(
                            TapGesture(count: 2)
                                .onEnded({ _ in
                                    withAnimation(.default) {
                                        scale.toggle()
                                    }
                                })
                        )
                    //长按
                    Image(systemName: "skateboard.fill")
                        .font(.system(size: 90))
                        .foregroundStyle(.pink)
                        .opacity(longGesture ? 0.3 : 1)
                        .animation(Animation.linear(duration: 2), value: longGesture)
                        .gesture(
                            LongPressGesture(minimumDuration: 2, maximumDistance: 50)
                                .updating($longGesture, body: { value, state, transaction in
                                    state = value
                                })
                        )
                    //拖拽
                    Image(systemName: "football.circle.fill")
                        .font(.system(size: 90))
                        .foregroundStyle(.pink, .yellow)
                        .offset(x: startPosition.width + dragState.width, y: startPosition.height + dragState.height)
                        .animation(.default, value: dragState)
                        .gesture(
                            DragGesture(minimumDistance:50).updating($dragState) { value, state, transaction in
                                state = value.translation
                            }.onEnded({ value in
                                
                                startPosition =  CGSize(width: startPosition.width + value.translation.width, height: startPosition.height + value.translation.height)
                            })
                        )
                    
                }
                
            }
        }.rotationEffect(rotate)
            .animation(.default, value: rotate)
            .scaleEffect(scaleF)
            .animation(.default, value: scaleF)
            .scaleEffect(magnify)
            .animation(.default, value: magnify)
            .gesture(MagnifyGesture().updating($magnify, body: { value, state, _ in
                state = value.magnification
            }))
        //         .gesture(
        //            MagnificationGesture().onChanged({ value in
        //                scaleF = value
        //            }).onEnded({ value in
        //                scaleF = 1
        //            })
        //         )
        //        .gesture(
        //            RotateGesture(minimumAngleDelta: Angle(degrees: 10)).updating($rotate, body: { value, state, transaction in
        //                state = value.rotation
        //            })
        //        )
        
    }
}

#Preview {
    Gesture()
}
